#include "StdAfx.h"
#include "HBGlobalTools.h"
#include <sstream>





std::string CHBGlobalTool::Int2Str( int iVal )
{
	stringstream ss;
	ss << iVal;

	string str;
	ss >> str;
	return str;
}

std::string CHBGlobalTool::TrimStr( string &sSrcStr, const string& sTargets )
{
	sSrcStr = TrimStrLeft(sSrcStr, sTargets);
	return TrimStrRight(sSrcStr, sTargets);
}

std::string CHBGlobalTool::TrimStrLeft( string &sSrcStr, const string& sTargets )
{
	if( sTargets.empty() || sSrcStr.empty())
	{
		return sSrcStr;
	}

	int iCurBeginPos = 0, iTargetsPos = 0;
	while(true)
	{
		iTargetsPos = sSrcStr.find(sTargets, iCurBeginPos);
		if (iTargetsPos == iCurBeginPos)
		{
			iCurBeginPos += sTargets.size();
		}
		else
		{
			break;
		}
	}

	sSrcStr = sSrcStr.substr(iCurBeginPos, sSrcStr.size() - iCurBeginPos);
	return sSrcStr;
}

std::string CHBGlobalTool::TrimStrRight( string &sSrcStr, const string& sTargets )
{
	if( sTargets.empty() || sSrcStr.empty())
	{
		return sSrcStr;
	}

	int iTargetsSize = sTargets.size();
	int iCurBeginPos = sSrcStr.size()-1, iTargetsPos = 0;
	while(true)
	{
		iTargetsPos = sSrcStr.rfind(sTargets, iCurBeginPos);
		if (iTargetsPos + iTargetsSize - 1 == iCurBeginPos)
		{
			iCurBeginPos -= sTargets.size();
		}
		else
		{
			break;
		}
	}

	sSrcStr = sSrcStr.substr(0, iCurBeginPos+1);
	return sSrcStr;
}

vecStr CHBGlobalTool::SplitStr( const string &str, const string &sepStr )
{
	vecStr vecSplitStr;
	int nSep =- 1;
	string tmp, strTemp = str;
	TrimStrLeft(strTemp, sepStr);
	TrimStrRight(strTemp, sepStr);
	while( !strTemp.empty() && ( nSep = strTemp.find( sepStr ) ) >  0 )
	{
		tmp = strTemp.substr( 0, nSep );
		strTemp = strTemp.substr( nSep, strTemp.size() - nSep );

		TrimStrLeft(strTemp, sepStr);
		TrimStrRight(strTemp, sepStr);
		TrimStrLeft(tmp, sepStr);
		TrimStrRight(tmp, sepStr);

		if( !tmp.empty() )
		{
			vecSplitStr.push_back( tmp );
		}
	}

	TrimStrLeft(strTemp, sepStr);
	TrimStrRight(strTemp, sepStr);

	if( !strTemp.empty() )
	{
		vecSplitStr.push_back( strTemp );
	}

	return vecSplitStr;
}